<?php
/***
* 我自己在一个AR9331 CPU上 定时运行 状态良好，为路由器而优化
***/
ini_set('log_errors','on');
ini_set('error_log',__dir__.DIRECTORY_SEPARATOR.'error.log');
class Cloudxns{
  const APIURL = 'https://www.cloudxns.net/api2/';
  const APIKEY = '';
  const SECRETKEY = '';
  private static $defaultoptions = [
    CURLOPT_HEADER => true, 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_FOLLOWLOCATION => 2,
    CURLOPT_SSL_VERIFYPEER => false, 
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_CONNECTTIMEOUT=> 30,
    CURLOPT_TIMEOUT=>45,
    CURLOPT_COOKIESESSION=>true,
    CURLOPT_ENCODING=>'UTF-8'
  ];
  private $options = [];
  private $ch;
  public $returninfo = [];


  public function __construct() {
   $this->ch = curl_init();
   $this->options = self::$defaultoptions;
  }
    
  private function fetch($header=FALSE){
    curl_setopt_array($this->ch, $this->options); 
    $result = curl_exec($this->ch);
    $this->returninfo = curl_getinfo($this->ch);
    //var_dump($this->returninfo);
    $this->options = self::$defaultoptions ;
    if($header){//显示头信息
     return $result;
    }
    $result = substr($result,  $this->returninfo['header_size']  );
    return $result;
  }

  public function testmd5(){
   $url = 'https://www.cloudxns.net/api2/api.php?action=md5';
   $this->options = $this->options + [
       CURLOPT_URL =>$url,
       CURLOPT_POST => TRUE,
   ]  ;
  }
  
  /**
   * 组装参数并请求
   * @param String $url
   * @param String $requestparameterstring
   * @param bool $header
   * @return type
   */
  private function assemblyoption( $url, $requestparameterstring='',$header=false){
    $rfctime = date( 'r' );
    $hmd5 = md5(self::APIKEY.$url.$requestparameterstring.$rfctime.self::SECRETKEY);
    $cxheader = [ 'API-KEY: '.self::APIKEY ];
    $cxheader[] = 'API-REQUEST-DATE: '.$rfctime;
    $cxheader[] = 'API-HMAC: '.$hmd5;
    $cxheader[] = 'API-FORMAT: json';
            
    $this->options +=  
    [
    CURLOPT_URL=>$url,
    CURLOPT_HTTPHEADER => $cxheader
    ]  ;
    
     return $this->fetch($header);
  }

  /**
   * 域名列表
   * @return type
   */
  public function domian(){
   $url = self::APIURL.'domain';//GET
   $this->options += [CURLOPT_POST => false] ;
   
   return $this->assemblyoption($url);
   
  }
  
    /**
   * 添加域名
   * @return type
   */
  public function domianadd($domain='fokite.com'){
   $url = self::APIURL.'domain';//POST
   $requestparameterarry = ['domain' => $domain];
   $requestparameterstring = json_encode($requestparameterarry);
   $this->options += [CURLOPT_POST => true] ;
   $this->options += [CURLOPT_POSTFIELDS=> $requestparameterstring];
   
   return $this->assemblyoption($url,$requestparameterstring);
   
  }

/**
 * 获取域名的解析列表
 * @param int $domainid
 * @param int $offset
 * @param int $rownum
 * @return String JSON
 */
  public function domainhostlist( $domainid, $offset=0, $rownum=2000){
  $url = self::APIURL."host/{$domainid}?offset={$offset}&row_num={$rownum}";//get
  
   $this->options += [CURLOPT_POST => false] ;
   return $this->assemblyoption($url);
  
  }
  
  /**
   * 获取解析记录的所有值
   * @param type $domainid
   * @param int $hostid 
   * @param type $offset
   * @param type $rownum
   * @return type
   */
  public function recordhostlist( $domainid,$hostid=0, $offset=0, $rownum=2000){
  $url = self::APIURL."record/{$domainid}?host_id={$hostid}&offset={$offset}&row_num={$rownum}";//get
  
   $this->options += [CURLOPT_POST => false] ;
   return $this->assemblyoption($url);
  
  }
  
  /**
   * 更新解析记录
   * @param type $rid 记录ID
   * @param type 域名ID
   * @param type 主机头
   * @param type 解析值
   * @param type $mx
   * @param type $ttl
   * @param type 记录类型
   * @param type 需要获取地区列表
   * @param type 备用IP
   * @return string
   */
  public function recordupdate($rid,$domainid,$host,$value,$mx=0,$ttl=0,$type='',$lineid=1,$bakip=''){
   $url = self::APIURL."record/{$rid}";//?PUT 
   $requestparameterarry = ['domain_id' => $domainid];
   $requestparameterarry['host'] = $host;
   $requestparameterarry['value'] = $value;
   $requestparameterarry['mx'] = $mx;
   if(!empty($ttl)) {$requestparameterarry['ttl'] = $ttl;}
   $requestparameterarry['type'] = $type;
   $requestparameterarry['line_id'] = $lineid;
   $requestparameterarry['bak_ip'] = $bakip;
   $requestparameterstring = json_encode($requestparameterarry);
    
   $this->options += [CURLOPT_CUSTOMREQUEST=>'PUT'];
   $this->options += [CURLOPT_POSTFIELDS=> $requestparameterstring];
   return $this->assemblyoption($url, $requestparameterstring);
  }
  
  /**
   * 
   * @param String 解析记录IP
   * @param String 域名ID
   * @param type 是否暂停，默认发送开启
   * @return type
   */
  public function pauserecord($listid,$domain,$pausema=FALSE){
   $url =  self::APIURL."record/pause";//POST
   $requestparameterarray = ['id'=>$listid];
   $requestparameterarray['domain_id'] = $domain;
   $requestparameterarray['pause']  = '1';
   if($pausema){$requestparameterarray['pause']  = '0';}
   
   $requestparameterstring = json_encode($requestparameterarray);
   echo $requestparameterstring;
   $this->options += [CURLOPT_POST => TRUE] ;
   $this->options += [CURLOPT_POSTFIELDS=> $requestparameterstring];
   
   return $this->assemblyoption($url, $requestparameterstring);
   
  }

  /**
   * 删除一个解析记录
   * @param type $listid
   * @return type
   */
  public function deleterecord($listid){
   $url  = self::APIURL."/host/{$listid}";
   
   $this->options += [CURLOPT_POST => false] ;
   $this->options += [CURLOPT_CUSTOMREQUEST=>'DELETE'];
   
   return $this->assemblyoption($url);
  }
  
    /**
   * 添加一个解析
   * @param type $domainid
   * @param type $value
   * @param type $lineid
   * @param type $host
   * @param type $type
   * @param type $mx
   * @param type $ttl
   * @return type
   */
  public function addrecord($domainid,$value,$lineid,$host='@',$type='A',$mx=0,$ttl=0){
   $url = self::APIURL."record";
   
   $requestparameterarry = ['domain_id' => $domainid];
   $requestparameterarry['host'] = $host;
   $requestparameterarry['value'] = $value;
   $requestparameterarry['type'] = $type;
   $requestparameterarry['mx'] = $mx;
   $requestparameterarry['line_id'] = $lineid;
   if(!empty($ttl)) {$requestparameterarry['ttl'] = $ttl;}
 //2016年3月29日3:26:24 居然出现BUG CLOUDxns TTL如果含有合法的数字值的时候就爆参数错误，如果不填 系统会给出一个JAVA INTMAX 在记录！
   $requestparameterstring = json_encode($requestparameterarry);
   
   $this->options += [CURLOPT_POST => TRUE] ;
   $this->options += [CURLOPT_POSTFIELDS=> $requestparameterstring];
   
   var_dump($url);
   var_dump($requestparameterstring);
   return $this->assemblyoption($url, $requestparameterstring);
  }
  
}
header("Content-type: text/html; charset=utf-8");
$s = new Cloudxns();
$ss =$s->domian() ;
echo '获取域名列表：'.($ss);
$ss = json_decode($ss,TRUE);
echo '<br>';
$domainid = $ss['data'][0]['id'];
$ss = $s->domainhostlist($ss['data'][0]['id']);
echo '获取域名记录列表：'.  ($ss);
$ss = json_decode($ss,TRUE);
$listid = $ss['hosts'][0]['id'];
//echo $s->recordupdate($listid, $domainid, 'xem', '223.5.5.6');

echo '<br>---获取某个解析的所有列表。---<br>';
$ss = $s->recordhostlist($domainid,$listid);
var_dump($ss);
$rid = json_decode($ss,TRUE);
$rid = $rid['data'][0];
var_dump($rid);
echo '|';
$rid = $rid['record_id'];
var_dump($rid);
echo '<br>----------';
echo $s->recordupdate($rid, $domainid, 'xem', '222.6.6.7');
echo '<br>---方滨兴死全家--<br>';
